﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.PowerSystem
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdDistributionCheck : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
            Autodesk.Revit.DB.View activeView = activeUIDocument.Document.ActiveView;
			EquipExchangeExternalHandler equipExchangeExternalHandler = new EquipExchangeExternalHandler(cmdData);
			ExternalEvent exEvent = ExternalEvent.Create(equipExchangeExternalHandler);
			if (!(activeView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((HYView.CmdViewType)3, cmdData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			bool flag = false;
			using (DlgDistributionCheck dlgDistributionCheck = new DlgDistributionCheck())
			{
				DialogResult dialogResult = dlgDistributionCheck.ShowDialog();
				if (dialogResult == DialogResult.Cancel)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				if (dialogResult == DialogResult.OK)
				{
					if (dlgDistributionCheck.rdoView.Checked)
					{
						flag = true;
					}
					else
					{
						flag = false;
					}
				}
			}
			if (flag)
			{
				new List<ElementId>();
				PanelSystemDetectCommand.GetAllElecEquips(activeUIDocument.Document, activeView.Id);
				if (PanelSystemDetectCommand.dlgDetect != null)
				{
					PanelSystemDetectCommand.dlgDetect.CurProj = false;
					PanelSystemDetectCommand.dlgDetect.InitData();
				}
				else
				{
					PanelSystemDetectCommand.dlgDetect = new DlgPanelSystemDetect(cmdData, equipExchangeExternalHandler, exEvent);
					PanelSystemDetectCommand.dlgDetect.CurProj = false;
					PanelSystemDetectCommand.dlgDetect.Show(WindowHelper.HWndRevit);
					WindowHelper.SendMessageToWndRevit();
				}
			}
			else
			{
				new List<Element>();
				PanelSystemDetectCommand.GetDocElecEquips(activeUIDocument.Document, ElementId.InvalidElementId);
				if (PanelSystemDetectCommand.dlgDetect != null)
				{
					equipExchangeExternalHandler.SetDetectDlg(ref PanelSystemDetectCommand.dlgDetect);
					PanelSystemDetectCommand.dlgDetect.InitData();
				}
				else
				{
					PanelSystemDetectCommand.dlgDetect = new DlgPanelSystemDetect(cmdData, equipExchangeExternalHandler, exEvent);
					equipExchangeExternalHandler.SetDetectDlg(ref PanelSystemDetectCommand.dlgDetect);
					PanelSystemDetectCommand.dlgDetect.Show(WindowHelper.HWndRevit);
					WindowHelper.SendMessageToWndRevit();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
